package both;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;


/**
 *
 * @author twoodf5611
 */

public abstract class AbstractFileListener {

    private Timer listenerTimer;
    private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();

    public AbstractFileListener() {

        ActionListener listenListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                try {
                    listenerTick();
                } catch (Exception e) {
                    // TODO ??????
                    e.printStackTrace();
               }
            }
        };
        listenerTimer = new Timer(100, listenListener);
    }

    protected abstract void listenerTick() throws Exception;

    public void startListening() {
        listenerTimer.start();
    }

    public void stopListening() {
        listenerTimer.stop();
    }

    public void addListener(ActionListener listen){
        listeners.add(listen);
    }

    public void removeListener(ActionListener listen) {
        listeners.remove(listen);
    }

    protected void sendEvent(ActionEvent ev) {
        for(ActionListener listener: listeners) {
            listener.actionPerformed(ev);
        }
    }
}
